/*
 * @Author: wuzhouhai wzh200x@126.com
 * @Date: 2024-05-29 15:59:32
 * @LastEditors: wuzhouhai wzh200x@126.com
 * @LastEditTime: 2024-12-05 10:53:52
 * @FilePath: \dwys_web\extensions\phoenix-plugin-client\assets\core\entities\Avatar.ts
 * @Description: 
 */
import { Opcode } from "../defines/opcode.define";
import { IModelData } from "../interface/iModelData";
import messageManager from "../managers/message.manager";
import { C2S_MessageLogin } from "../net/message/gameMessage/c2s/c2sMessage_login";
import * as proto from "../protoc/ts/proto.js";
import Entity from "./Entity";

export class Avatar extends Entity {
    private avatarName:string;
    private avatarModelData:IModelData|null = null;
    private createTime:number = 0;
    private updateTime:number = 0;
    private credential:string = "";
    private iv:string = "";
    private modules:Map<number, any> = new Map<number, any>(); // 模块数据
    constructor(name:string = ""){
        super();
        this.avatarName = name;
    }

    public registerMessageHandle(){
    }

    public getAvatarName():string{
        return this.avatarName;
    }

    public setAvatarName(name:string):void{
        this.avatarName = name;
    }

    public setAvatarDataModel(data:IModelData):void{
        this.avatarModelData = data;
    }

    public getAvatarDataModel():any{
        return this.avatarModelData;
    }

    public SerializeData():string{
        return this.avatarModelData?.SerializeData() || "";
    }

    public DeserializationData(data:string, useServer:boolean):void{
        this.avatarModelData?.Deserialization(data, useServer);
    }

    public getCreateTime():number{
        return this.createTime;
    }

    public setCreateTime(time:number):void{
        this.createTime = time;
    }

    public getUpdateTime():number{
        return this.updateTime;
    }

    public setUpdateTime(time:number):void{
        this.updateTime = time;
    }

    public setCredential(c:string){
        this.credential = c;
    }

    public getCredential():string{
        return this.credential;
    }

    public setIv(iv:string){
        this.iv = iv;
    }

    public getIv():string{
        return this.iv;
    }

    public checkModuelIsExist(moduleId:number):boolean{
        return this.modules.has(moduleId);
    }

    public setModuleDataFromServer(moduleId:number, data:string){
        console.log("setModuleDataFromServer", moduleId, data);
        this.modules.set(moduleId, JSON.parse(data));
    }

    // 序列化模块数据
    public serializeModuleData(moduleId:number):string{
        return JSON.stringify(this.modules.get(moduleId));
    }

    public setModuleData(moduleId:number, data:any){
        this.modules.set(moduleId, data);
    }

    public getModuleData(moduleId:number):any{
        return this.modules.get(moduleId);
    }

    public SendLogin():void{
        let packet = messageManager.createPacket(Opcode.Opcode_CS_LOGIN);
        let message = packet.getMessage<C2S_MessageLogin>();
        message.SetData(this.getCredential(), this.getIv())
        messageManager.pushPacket(packet, null);
    }

    public OnLoginHandle(msgObject:any){
        let loginResponse = msgObject as proto.s2c_login;
        console.log("OnLoginHandle", loginResponse);
    }
}